package com.pinapel.aixml.ui.actions;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTree;

import com.pinapel.aixml.controller.MainController;
import com.pinapel.aixml.tools.LookAndFeel;
import com.pinapel.aixml.tools.Printer;
import com.pinapel.aixml.tools.i18n.I18n;
import com.pinapel.aixml.tools.log.EnumMessageType;
import com.pinapel.aixml.tools.log.Logger;

public class PrintAction extends JFrame {
	private JRadioButton jXSDTree;
	private JRadioButton jXMLTree;
	private JButton bPrint;
	private JButton bClose;

	public PrintAction(){
		super();
		LookAndFeel.setLookAndFeel("Nimbus");

		this.setTitle(I18n.getString(I18n.UI056_PRINT_TITLE));

		jXSDTree = new JRadioButton(I18n.getString(I18n.UI058_XSD_TREE));
		jXMLTree = new JRadioButton(I18n.getString(I18n.UI059_XML_TREE));
		bPrint = new JButton(I18n.getString(I18n.UI057_PRINT_BUTTON));
		bClose = new JButton(I18n.getString(I18n.UI042_CLOSE));
		this.setLayout(new BorderLayout());
		this.add(jXMLTree);
		this.add(jXSDTree);
		ButtonGroup bg = new ButtonGroup();

		bg.add(jXSDTree);
		bg.add(jXMLTree);
		jXMLTree.setSelected(true);
		this.add(buttonsPanel(), BorderLayout.SOUTH);
		this.add(checkPanel(), BorderLayout.CENTER);
		pack();
		setVisible(true);
		setAlwaysOnTop(true);
		this.setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	private JPanel checkPanel() {
		JPanel res = new JPanel();

		res.setLayout(new FlowLayout(FlowLayout.CENTER));

		res.add(jXSDTree);
		res.add(jXMLTree);

		return res;
	}

	private JPanel buttonsPanel() {
		JPanel res = new JPanel();

		/* Adding methods to buttons */
		bPrint.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// Create a save dialog
				SaveFile sv = new SaveFile(new ImageFilter());
				// The user selected a approve option
				if (sv.getOption() == JFileChooser.APPROVE_OPTION) {
					File file = sv.getSelectedFile();
					// The file descriptor is not null
					if (file != null) {
						// The file already exists
						if (file.exists()) {
							Logger.addMessage(EnumMessageType.DEBUG_INFO, "The file already exists.");
							int optionSelected = JOptionPane.showConfirmDialog(
									null,
									I18n.getString(I18n.UI051_SAVEAS_DIALOG_BOX_TEXT),
									I18n.getString(I18n.UI030_SAVE_AS),
									JOptionPane.YES_NO_OPTION);
							// Don't want overwrite it, then exit
							if (optionSelected == JOptionPane.NO_OPTION) {
								Logger.addMessage(EnumMessageType.DEBUG_INFO, "File has not been overwriten");
								return;
							}
						}
						// Get the tree from the view
						JTree tree = null;
						Logger.addMessage(EnumMessageType.DEBUG_INFO, "Getting the tree from view.");
						if (jXMLTree.isSelected()) {
							tree = MainController.getInstance()
									.getTreeController().getXMLTreeController()
									.getTreeView().getJTree();
						} else if (jXSDTree.isSelected()) {
							tree = MainController.getInstance()
									.getTreeController().getXSDTreeController()
									.getTreeView().getJTree();
						}
						// Print the tree
						Printer.printTree(tree, file);
						// Then close this windows
						dispose();
						Logger.addMessage(EnumMessageType.INFO, "Success! Image saved.");
					}
				}
			}
		});
		bClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				/* CERRAMOS VENTANA */
				dispose();
			}
		});
		res.setLayout(new FlowLayout(FlowLayout.CENTER));

		res.add(bPrint);
		res.add(bClose);

		return res;
	}
}
